﻿using System;
using System.Collections.Generic;
using UnityEngine;
using VoxelBlock;

public class TransformScaleBox : MonoBehaviour {

    [SerializeField]
    Transform up;
    [SerializeField]
    Transform down;
    [SerializeField]
    Transform left;
    [SerializeField]
    Transform right;
    [SerializeField]
    Transform front;
    [SerializeField]
    Transform breaks;

    [SerializeField] UIToggle uiTransform;
    [SerializeField] UIToggle uiSelect;
    [SerializeField] UIToggle uiBursh;
    [SerializeField] UIToggle uiScale;

    private static TransformScaleBox thas;

    public VoxelDirection direction;

    public static TransformScaleBoxUnit selectUnit;

    public static TransformScaleBox Thas
    {
        get
        {
            return thas;
        }
    }

    private void Awake()
    {
        if (thas == null)
        {
            thas = this;
        }
        this.transform.root.gameObject.SetActive(false);
    }

    public Action<Vector3Int, Vector3Int> NowSelect;
    public Action Over;
    public void MyOver()
    {
        uiSelect.value = true;
        EventDelegate.Execute(uiSelect.GetComponent<UIButton>().onClick);

        uiScale.Set(false);

        uiBursh.Set(true);
        EventDelegate.Execute(uiBursh.GetComponent<UIButton>().onClick);

        //EventDelegate.Execute(uiScale.GetComponent<UIButton>().onClick);
    }

     public void Show(Vector3Int positionA, Vector3Int positionB)
    {
        uiTransform.value = true;
        EventDelegate.Execute(uiTransform.GetComponent<UIButton>().onClick);
        TransformVector.Thas.Look = null;
        //if (TransformVector.selectUnit)
        //{
        //    TransformVector.selectUnit.NoSelect();
        //}
        //UIItemTransformUnitComponent.UnLookThis();
        //TransformVector.Thas.gameObject.SetActive(false);

        if (positionA.x > positionB.x)
        {
            int timp = positionB.x;
            positionB.x = positionA.x;
            positionA.x = timp;
        }
        if (positionA.y > positionB.y)
        {
            int timp = positionB.y;
            positionB.y = positionA.y;
            positionA.y = timp;
        }
        if (positionA.z > positionB.z)
        {
            int timp = positionB.z;
            positionB.z = positionA.z;
            positionA.z = timp;
        }

        right.position = new Vector3(positionB.x + 1, right.position.y, right.position.z);
        up.position = new Vector3(up.position.x, positionB.y + 1, up.position.z);
        front.position = new Vector3(front.position.x, front.position.y, positionB.z + 1);

        left.position = new Vector3(positionA.x, left.position.y, left.position.z);
        down.position = new Vector3(down.position.x, positionA.y, down.position.z);
        breaks.position = new Vector3(breaks.position.x, breaks.position.y, positionA.z);
    }
	
	void Update () {
        float px = 0;
        float py = 0;
        float pz = 0;
        px = (left.position.x + right.position.x)/2;
        py = (up.position.y + down.position.y)/2;
        pz = (front.position.z + breaks.position.z)/2;
        this.transform.position = new Vector3(px, py, pz);

        up.position = new Vector3(px, up.position.y, pz);
        down.position = new Vector3(px, down.position.y, pz);

        left.position = new Vector3(left.position.x, py, pz);
        right.position = new Vector3(right.position.x, py, pz);

        front.position = new Vector3(px, py, front.position.z);
        breaks.position = new Vector3(px, py, breaks.position.z);


        float sx = 0;
        float sy = 0;
        float sz = 0;
        sy = Mathf.Abs((up.position.y - down.position.y));
        sx = Mathf.Abs((left.position.x - right.position.x) );
        sz = Mathf.Abs((front.position.z - breaks.position.z) );
        this.transform.localScale = new Vector3(sx, sy, sz);

        if (NowSelect != null)
        {
            NowSelect(new Vector3Int(right.position.x - 1, up.position.y - 1, front.position.z - 1) , new Vector3Int(left.position.x, down.position.y, breaks.position.z));
        }

        //var arrayPoint = BasicAlgorithm.GetRectanglePointArray(arg.gestureStartPosition, rectangleOldPosition);

        //        nowDrawVoxel3DCore.DrawSelect(oldSerializeVoxel, 255);
        //        oldSerializeVoxel = nowDrawVoxel3DCore.DrawSelect(arrayPoint, 1);
        }
}
